import React, { Component } from "react";
import PubSub from "pubsub-js";
import axios from "axios";

export default class Search extends Component {
  search = () => {
    //获取用户的输入
    const {
      keyWordElement: { value: keyWord },
    } = this;
    // console.log(keyWord);
    // https://api.github.com/search/users?q=${keyWord}
    // this.props.updateAppState({ isFirst: false, isLoading: true });
    PubSub.publish("tk", { isFirst: false, isLoading: true });
    //发送网络请求
    axios.get(`/api/search/users?q=${keyWord}`).then(
      (response) => {
        PubSub.publish("tk", { isLoading: false, users: response.data.items });
        // this.props.updateAppState({
        //   isLoading: false,
        //   users: response.data.items,
        // });
      },
      (error) => {
        PubSub.publish("tk", { isLoading: false, err: error.message });
        // this.props.updateAppState({
        //   isLoading: false,
        //   err: error.message,
        // });
      }
    );
  };

  render() {
    return (
      <div>
        <section className="jumbotron">
          <h3 className="jumbotron-heading">搜索Github用户</h3>
          <div>
            <input
              ref={(c) => (this.keyWordElement = c)}
              type="text"
              placeholder="输入关键词点击搜索"
            />
            &nbsp;<button onClick={this.search}>搜索</button>
          </div>
        </section>
      </div>
    );
  }
}
